// pages/home/shopDynamic/shopDynamic.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArr: [],
    pageNum:'1',
    shopId:'',//商户的ID
    imgURL: app.globalData.imgURL,//图片总路径
    shopInfo:{},//商户信息
  },

  phoneClick: function (e) {
    console.log(e.currentTarget.id);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id,
    })
  },

  //图片点击
  imgClick: function (e) {
    var that = this;
    console.log(e);
    console.log(e.currentTarget.id);
    console.log(e.currentTarget.dataset.hang);
    var imgArr = that.data.listArr[e.currentTarget.id].img_url;
    var nImgArr = new Array();
    for (var i = 0; i < imgArr.length; i++) {
      nImgArr[i] = that.data.imgURL + imgArr[i].replace('_thum', '');
    }
    wx.previewImage({
      current: nImgArr[e.currentTarget.dataset.hang],
      urls: nImgArr,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    this.setData({
      shopId: options.id,
    })

    this.getData();
  },

  getData: function () {
    var that = this;
    app.post('act/listForShop', {
      pageNum: that.data.pageNum, openId:that.data.shopId}).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        var result = res.actives;
        for (var i = 0; i < result.length; i++) {
          result[i].img_url = result[i].img_url.split(',');
        }
        console.log(result);
        // that.setData({
        //   listArr: result,
        //   shopInfo:res.shopInfo,
        // })
        that.setData({
          listArr: that.data.listArr.concat(result),
          shopInfo: res.shopInfo,
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多');
    var that = this;
    that.data.pageNum++;
    console.log(that.data.pageNum);
    that.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})